package com.njtoyo.taxi.admin.handler.common;

import cn.hutool.core.date.DateUtil;
import com.njtoyo.taxi.admin.mapper.master.entity.*;
import com.njtoyo.taxi.admin.mapper.master.enums.*;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverCustomMessageMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverFlowLogMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverRewardLogMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverRewardMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * 司机发放奖励
 */
@Component
@Slf4j
public class DriverRewardHandler {
    @Autowired
    private DriverRewardMapper driverRewardMapper;
    @Autowired
    private DriverCustomMessageMapper driverCustomMessageMapper;
    @Autowired
    private DriverRewardLogMapper driverRewardLogMapper;
    @Autowired
    private DriverFlowLogMapper driverFlowLogMapper;

    @Transactional(rollbackFor = Exception.class)
    public Long sendDriverReward(Long driverId, BigDecimal rewardAmount) {
        DriverReward driverReward = new DriverReward()
                .setDriverId(driverId)
                .setAmount(rewardAmount)
                .setCategory(DriverRewardCategory.recommend)
                .setArrivalTime(DateUtil.date())
                .setIsVerified(true)
                .setThrough("other")
                .setMarketingId(2L);
        driverRewardMapper.insert(driverReward);

        DriverRewardLog driverRewardLog = new DriverRewardLog()
                .setAmount(rewardAmount)
                .setDriverId(driverId);
        driverRewardLogMapper.insert(driverRewardLog);


        DriverCustomMessage driverCustomMessage = new DriverCustomMessage()
                .setDriverId(driverId)
                .setContent("您收到了系统发放的奖励" + rewardAmount + "元")
                .setIsPlainText(true)
                .setLinkCategory(DriverCustomMessageLinkCategory.self)
                .setEventCategory(DriverCustomMessageEventCategory.system);
        driverCustomMessageMapper.insert(driverCustomMessage);


        DriverFlowLog driverFlowLog = new DriverFlowLog().setDriverId(driverId)
                .setAmount(rewardAmount)
                .setCategory(DriverFlowLogCategory.ride_red_packet)
                .setInOut(DriverFlowLogInOut.in);
        driverFlowLogMapper.insert(driverFlowLog);

        return driverReward.getId();
    }
}
